package com.seal.tsms.base.service;

import com.seal.tsms.base.dao.QueryParser;
import com.seal.tsms.base.dao.Querys;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by seal.jing on 2015/12/26.
 */
public class ParamsFilter {
    public static void filter(Map<String,?> params){

        Set<String> keys=params.keySet();
        for (String key:keys ) {
            if(key.startsWith(Querys.PREFIX)||key.equals(Querys.ORDER_ATTR)||key.equals(Querys.ORDER_TYPE)){
               params.remove(key);
            }
        }

    }
    public static String build(String dsl,Map<String,?> params){
        try {
            String dsl2= QueryParser.getQueryString(dsl,params);
            if(StringUtils.isNotBlank(dsl2)){
                dsl=dsl2;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        ParamsFilter.filter(params);
        return dsl;
    }
    public static String buildCount(String hql){
        String count_hql="select count(*) "+hql.substring(hql.toLowerCase().indexOf("from"));
        return count_hql;
    }

}
